package com.css.fxfzaqbz.modules.maps.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.BigDisasterEntity;
import com.css.fxfzaqbz.modules.callJcData.entity.Gasholder;
import com.css.fxfzaqbz.modules.callJcData.param.GasholderQueryParams;
import com.css.fxfzaqbz.modules.maps.vo.MapQueryVO;
import com.css.fxfzaqbz.modules.maps.vo.MapUploadVO;
import com.css.fxfzaqbz.modules.maps.vo.MapViewVO;
import com.css.fxfzaqbz.modules.maps.entity.MapType;
import com.css.fxfzaqbz.modules.maps.entity.ProfessionMap;
import com.css.fxfzaqbz.modules.maps.repository.MapManagerRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年02月07日  11:20
 */
@Repository
public class MapManagerRepositoryImpl implements MapManagerRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Override
    public List<ProfessionMap> page(MapQueryVO param) {
        StringBuilder sqlBuilder = new StringBuilder("select map.* from sys_profession_map map where 1=1 ");
        pageSQLBuilder(param, sqlBuilder);
        sqlBuilder.append(" order by map.upload_time desc ").append("limit ").append(param.getRows()).append(" offset ").append(param.getPage());
        return jdbcTemplate.query(sqlBuilder.toString(), new BeanPropertyRowMapper<>(ProfessionMap.class));
    }

    @Override
    public Integer countForPage(MapQueryVO param) {
        StringBuilder sqlBuilder = new StringBuilder("select count(map.*) from sys_profession_map map where 1=1  ");
        pageSQLBuilder(param, sqlBuilder);
        return jdbcTemplate.queryForObject(sqlBuilder.toString(), Integer.class);
    }

    private void pageSQLBuilder(MapQueryVO param, StringBuilder sqlBuilder) {
        if (StringUtils.isNotBlank(param.getMapName())) {
            sqlBuilder.append("and map.name = '").append(param.getMapName()).append("' ");
        }
        if (StringUtils.isNotBlank(param.getMapType())) {
            sqlBuilder.append("and map.type = '").append(param.getMapType()).append("' ");
        }
        sqlBuilder.append("and map.del_flag = '").append(YNEnum.N.toString()).append("' ");
    }

    @Override
    public List<MapType> getMapType() {
        String sql = "select * from sys_map_type where del_flag='"+YNEnum.N.toString()+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MapType.class));
    }

    @Override
    public void del(List<String> ids) {
        String sql = "update sys_profession_map set del_flag='"+YNEnum.Y.toString()+"' where id in (:ids)";
        Map map = new HashMap(1);
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public void uploadSave(MapUploadVO vo) {
        String sql = "insert into sys_profession_map(" +
                "id,type,map_desc," +
                "upload_user_id,upload_user_name,upload_time," +
                "del_flag,attach_id,name,template_name)" +
                " values (" +
                ":id,:type,:mapDesc,:uploadUserId," +
                ":uploadUserName,:uploadTime," +
                ":delFlag,:attachId,:name,:templateName)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(vo);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public MapViewVO view(String id) {
        String sql = "select id,type,map_desc,name,attach_id,template_name from sys_profession_map where del_flag='"+YNEnum.N.toString()+"' and id='"+id+"'";
        return jdbcTemplate.queryForObject(sql,  new BeanPropertyRowMapper<>(MapViewVO.class));
    }

    @Override
    public void update(MapViewVO vo) {
        String sql = "update sys_profession_map set name='"+vo.getName()+"',map_desc='"+vo.getMapDesc()+"' " +
                ",update_user_id='"+vo.getUpdateUserId()+"',update_time='"+vo.getUpdateTime()+"' where id='"+vo.getId()+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public Integer getProfessionMapMsg(String type, String name) {
        String sql = "select count(*) from sys_profession_map where del_flag='"+YNEnum.N.toString()+"' and name='"+name+"' and type='"+type+"'";
        return jdbcTemplate.queryForObject(sql,  Integer.class);
    }

    @Override
    public List<MapViewVO> getTypeNameByType(String type) {
        String sql = "select * from sys_profession_map where del_flag='"+YNEnum.N.toString()+"' and type='"+type+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MapViewVO.class));
    }
}
